package at.htlkaindorf.jsf.session;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "languageBean")
@SessionScoped
public class LanguageBean {

    private Locale currentLocale = Locale.ENGLISH;
    private Map<String, Locale> countryMap;

    public LanguageBean() {
        countryMap = new LinkedHashMap<String, Locale>();
        countryMap.put("Deutsch", Locale.GERMANY);
        countryMap.put("English", Locale.ENGLISH);
    }

    public void changeLocale(String locale) {
        for (Map.Entry<String, Locale> entry : countryMap.entrySet()) {
            if (entry.getValue().toString().equals(locale)) {
                currentLocale = entry.getValue();
                FacesContext.getCurrentInstance().getViewRoot().setLocale(entry.getValue());
            }
        }
    }

    public Locale getCurrentLocale() {
        return currentLocale;
    }

    public void setCurrentLocale(Locale currentLocale) {
        this.currentLocale = currentLocale;
    }
}
